package com.ts.api.module.visitor.service.impl;

import com.gitee.apanlh.util.date.DateUtils;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.common.context.ChannelRequestContext;
import com.ts.api.common.context.ChannelUserContext;
import com.ts.api.module.channel.entity.ChannelRequest;
import com.ts.api.module.channel.entity.cache.ChannelCacheUser;
import com.ts.api.module.channel.entity.po.ChannelMarketVisitorPO;
import com.ts.api.module.visitor.dao.ChannelVisitorDao;
import com.ts.api.module.visitor.entity.ChannelVisitorPv;
import com.ts.api.module.visitor.entity.ChannelVisitorUv;
import com.ts.api.module.visitor.service.ChannelVisitorService;

import java.math.BigDecimal;
import java.time.LocalDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 *  渠道访问业务层
 */
@Service
public class ChannelVisitorServiceImpl implements ChannelVisitorService {

    @Autowired
    private ChannelVisitorDao channelVisitorDao;

    @Override
    public void insertVisitor() {
        ChannelRequest channelRequest = ChannelRequestContext.get();
        Long channelId = ChannelContext.getId();

        ChannelVisitorPv channelVisitorPv = new ChannelVisitorPv();
        channelVisitorPv.setCreateTime(DateUtils.currentTime());
        channelVisitorPv.setLoanChannelId(channelId);
        channelVisitorPv.setIp(channelRequest.getIp());
        channelVisitorPv.setBrowserName(channelRequest.getUserAgent().getBrowser().getName());
        channelVisitorPv.setOsName(channelRequest.getUserAgent().getOperatingSystem().getName());
        channelVisitorPv.setDeviceType(channelRequest.getUserAgent().getOperatingSystem().getDeviceType().getName());
        channelVisitorDao.insertPv(channelVisitorPv);


        try {
            ChannelVisitorUv channelVisitorUv = new ChannelVisitorUv();
            channelVisitorUv.setCreateTime(DateUtils.currentTime());
            channelVisitorUv.setLoanChannelId(channelId);
            channelVisitorUv.setIp(channelRequest.getIp());
            channelVisitorUv.setBrowserName(channelRequest.getUserAgent().getBrowser().getName());
            channelVisitorUv.setOsName(channelRequest.getUserAgent().getOperatingSystem().getName());
            channelVisitorUv.setDeviceType(channelRequest.getUserAgent().getOperatingSystem().getDeviceType().getName());
            channelVisitorUv.setCreateDate(LocalDate.now());
            channelVisitorDao.insertUv(channelVisitorUv);
        } catch (DuplicateKeyException e) {
            // do nothing
        }
    }

    @Override
    public void insertMarketVisitor(Long loanMarketId) {
        Long channelId = ChannelContext.getId();
        Long userId = ChannelUserContext.get().getUserId();

        this.insertMarketVisitor(channelId, userId, loanMarketId);
    }

    @Override
    public void insertMarketVisitor(Long channelId, Long userId, Long loanMarketId) {
        ChannelRequest channelRequest = ChannelRequestContext.get();

        BigDecimal price = this.channelVisitorDao.findMarketPriceById(loanMarketId);

        ChannelMarketVisitorPO marketVisitorPO = new ChannelMarketVisitorPO();
        marketVisitorPO.setCreateTime(DateUtils.currentTime());
        marketVisitorPO.setLoanChannelId(channelId);
        marketVisitorPO.setLoanMarketId(loanMarketId);
        marketVisitorPO.setPrice(price);
        marketVisitorPO.setLoanUserId(userId);
        marketVisitorPO.setIp(channelRequest.getIp());
        marketVisitorPO.setBrowserName(channelRequest.getUserAgent().getBrowser().getName());
        marketVisitorPO.setOsName(channelRequest.getUserAgent().getOperatingSystem().getName());
        marketVisitorPO.setDeviceType(channelRequest.getUserAgent().getOperatingSystem().getDeviceType().getName());

        channelVisitorDao.insertMarketVisitor(marketVisitorPO);
    }
}
